package com.casstime.ec.cloud.meeting.service;


import com.casstime.commons.model.Result;
import com.casstime.commons.service.exception.HttpMessageException;
import com.casstime.ec.cloud.meeting.model.BookDetail;
import com.casstime.ec.cloud.meeting.model.Department;
import com.casstime.ec.cloud.meeting.model.Device;
import com.casstime.ec.cloud.meeting.model.MeetingRoom;
import com.casstime.ec.cloud.meeting.service.handler.BookDeatilHandler;
import com.casstime.ec.cloud.meeting.service.handler.DeviceHandler;
import com.casstime.ec.cloud.meeting.service.handler.MeetingRoomHandler;
import com.casstime.ec.cloud.meeting.service.handler.UserHandler;
import com.casstime.ec.cloud.meeting.values.BookDetailDTO;
import com.casstime.ec.cloud.meeting.values.BookDetailQuery;
import com.casstime.ec.cloud.meeting.values.BookMeetingRoomParam;
import com.casstime.ec.cloud.meeting.values.BookRoomQuery;
import com.casstime.ec.cloud.meeting.values.BookRoomResult;
import com.casstime.ec.cloud.meeting.values.DepartmentDTO;
import com.casstime.ec.cloud.meeting.values.EquipDTO;
import com.casstime.ec.cloud.meeting.values.MeetingRoomDTO;
import com.casstime.ec.cloud.meeting.values.MeetingRoomQuery;
import com.casstime.ec.cloud.meeting.values.MeetingRoomSaveParam;
import com.casstime.ec.cloud.meeting.values.ResultBookMeetingRoom;
import com.casstime.ec.cloud.meeting.values.ResultDeleteMyBook;
import com.casstime.ec.cloud.meeting.values.UserDTO;
import com.casstime.ec.cloud.service.GenericService;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin

@RestController
@Log4j2
public class MeetingServiceImpl extends GenericService implements MeetingService {

  @Autowired
  private MeetingRoomHandler meetingRoomHandler;

  @Autowired
  private BookDeatilHandler bookDeatilHandler;

  @Autowired
  private UserHandler userHandler;

  @Autowired
  private DeviceHandler deviceHandler;


  @Override
  public MeetingRoomDTO saveMeetingRoom(@RequestBody MeetingRoomSaveParam meetingRoomSaveParam)
      throws Exception {
    log.info(meetingRoomSaveParam);
    if (meetingRoomSaveParam == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    MeetingRoom meetingRoom = meetingRoomHandler.saveMeetingRoom(meetingRoomSaveParam);
    return meetingRoom.toDTO();
  }


  @Override
  public List<MeetingRoomDTO> getMeetingRoomList() throws Exception {
    List<MeetingRoomDTO> meetingRoomDTOList = new ArrayList<MeetingRoomDTO>();
    List<MeetingRoom> meetingRoomList = meetingRoomHandler.getMeetingRoomList();
    for (MeetingRoom meetingRoom : meetingRoomList) {
      meetingRoomDTOList.add(meetingRoom.toDTO());
    }
    return meetingRoomDTOList;
  }


  @Override
  public MeetingRoomDTO getMeetingRoomById(@RequestParam("meetingRoomId") Integer meetingRoomId)
      throws Exception {
    log.info(meetingRoomId);
    if (meetingRoomId == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    MeetingRoom meetingRoom = meetingRoomHandler.getMeetingRoomById(meetingRoomId);
    return meetingRoom.toDTO();
  }


  @Override
  public List<MeetingRoomDTO> getMeetingRoomByName(
      @RequestParam("meetingRoomName") String meetingRoomName) throws Exception {
    log.info(meetingRoomName);
    if (meetingRoomName == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    List<MeetingRoomDTO> meetingRoomDTOList = new ArrayList<MeetingRoomDTO>();
    List<MeetingRoom> meetingRoomList = meetingRoomHandler.getMeetingRoomByName(meetingRoomName);
    for (MeetingRoom meetingRoom : meetingRoomList) {
      meetingRoomDTOList.add(meetingRoom.toDTO());
    }
    return meetingRoomDTOList;
  }


  @Override
  public List<MeetingRoomDTO> getMeetingRoomByStatus(
      @RequestParam("meetingRoomNameStatus") Integer meetingRoomNameStatus) throws Exception {
    log.info(meetingRoomNameStatus);
    if (meetingRoomNameStatus == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    List<MeetingRoomDTO> meetingRoomDTOList = new ArrayList<MeetingRoomDTO>();
    List<MeetingRoom> meetingRoomList = meetingRoomHandler
        .getMeetingRoomByStatus(meetingRoomNameStatus);
    for (MeetingRoom meetingRoom : meetingRoomList) {
      meetingRoomDTOList.add(meetingRoom.toDTO());
    }
    return meetingRoomDTOList;
  }


  @Override
  public List<MeetingRoomDTO> getMeetingRoomListByQuery(
      @RequestBody MeetingRoomQuery meetingRoomQuery) throws Exception {
    return null;
  }


  @Override
  public List<BookRoomResult> getBookRooms(BookRoomQuery roomQuery) throws Exception {
    return null;
  }

  @Override
  public BookDetailDTO changeBookRoom(BookMeetingRoomParam roomQuery) throws Exception {
    return null;
  }


  @Override
  public BookDetailDTO getBookDetailById(@RequestParam("bookDetailId") Integer bookDetailId)
      throws Exception {
    log.info(bookDetailId);
    if (bookDetailId == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    BookDetail bookDetail = bookDeatilHandler.getBookDetailById(bookDetailId);
    return bookDetail.toDTO();
  }


  @Override
  public List<BookDetailDTO> getBookDetailList() throws Exception {
    List<BookDetailDTO> bookDetailDTOList = new ArrayList<BookDetailDTO>();
    List<BookDetail> list = bookDeatilHandler.getBookDetailList();
    if (list.size() == 0) {
      return null;
    }
    for (BookDetail bookDetail : list) {
      bookDetailDTOList.add(bookDetail.toDTO());
    }
    return bookDetailDTOList;

  }


  @Override
  public UserDTO userLogin(@RequestBody UserDTO user) throws Exception {
    return userHandler.login(user);
  }


  @Override
  public int removeMeetingRoomByName(@RequestParam("roomName") String roomName) {
    int result = meetingRoomHandler.removeMeetingRoomByName(roomName);
    return result;
  }


  /**
   * 按指定日期和会议室查询当天的预约详情
   */
  @Override
  public List<BookDetailDTO> getBookDetailByQuery(@RequestBody BookDetailQuery bookDetailQuery)
      throws Exception {
    log.info(bookDetailQuery);
    if (bookDetailQuery == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    //如果用户没有选择时间，则选取当前系统的时间
    if (bookDetailQuery.getDate() == null) {
      Date date = new Date();
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      String currentDate = format.format(date);
      bookDetailQuery.setDate(currentDate);
    }
    List<BookDetail> bookDetailList = bookDeatilHandler.getBookDetailByQuery(bookDetailQuery);
    List<BookDetailDTO> bookDetailDTOList = new ArrayList<BookDetailDTO>();
    if (bookDetailList == null) {
      return null;
    } else {
      for (BookDetail bookDetail : bookDetailList) {
        bookDetailDTOList.add(bookDetail.toDTO());
      }
      return bookDetailDTOList;
    }

  }


  @Override
  public List<BookDetailDTO> getBookDetailByUserId(@RequestParam("id") Integer userId)
      throws Exception {
    log.info(userId);
    if (userId == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    List<BookDetail> bookDetailList = bookDeatilHandler.getBookDetailByUserId(userId);
    List<BookDetailDTO> bookDetailDTOList = new ArrayList<BookDetailDTO>();
    if (bookDetailList == null) {
      return null;
    } else {
      for (BookDetail bookDetail : bookDetailList) {
        bookDetailDTOList.add(bookDetail.toDTO());
      }
      return bookDetailDTOList;
    }

  }


  @Override
  public DepartmentDTO getDepartmentByUserId(@RequestParam("userId") Integer userId)
      throws Exception {
    Department department = null;
    log.info(userId);
    if (userId == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    department = userHandler.getDepartmentByUserId(userId);
    if (department == null) {
      return null;
    }
    return department.toDTO();
  }


  @Override
  public List<EquipDTO> getEquipByRoomId(@RequestParam("roomId") Integer roomId) throws Exception {
    List<EquipDTO> equipDTOList = new ArrayList<EquipDTO>();
    if (roomId == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    List<Device> deviceList = deviceHandler.getEquipByRoomId(roomId);
    if (deviceList == null) {
      return null;
    }
    for (Device device : deviceList) {
      equipDTOList.add(device.toDTO());
    }
    return equipDTOList;
  }


  /**
   * 预约会议室
   */
  @Override
  public ResultBookMeetingRoom bookMeetingRoom(
      @RequestBody BookMeetingRoomParam bookMeetingRoomParam) throws Exception {
    ResultBookMeetingRoom resultBookMeetingRoom = null;
    log.info(bookMeetingRoomParam);
    if (bookMeetingRoomParam == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    try {
      resultBookMeetingRoom = bookDeatilHandler.bookMeetingRoom(bookMeetingRoomParam);
    } catch (Exception e) {
      log.error("预约会议室失败", e);
      Result.error("服务器错误");
    }
    return resultBookMeetingRoom;
  }


  /**
   * 删除一条我的预约信息
   * @param bookId
   * @return
   * @throws Exception
   */
  @Override
  public ResultDeleteMyBook deleteMyBook(@RequestParam("bookId") Integer bookId) throws Exception {
      if(bookId == null) {
          throw new HttpMessageException(417, "参数不能为空");
      }
      ResultDeleteMyBook resultDeleteMyBook = null;
      resultDeleteMyBook = bookDeatilHandler.deleteMyBook(bookId);
      return resultDeleteMyBook;

  }


}
